package com.wm.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wm.commonutils.result.ResultCode;
import com.wm.eduService.client.VodClient;
import com.wm.eduService.entity.EduVideo;
import com.wm.eduService.mapper.EduVideoMapper;
import com.wm.eduService.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wm.servicebase.exceptionhandle.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author sshdg
 * @since 2020-10-01
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    VodClient vodClient;

    @Override
    public void deleteVideoByCourseId(String courseId) {
        LambdaQueryWrapper<EduVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getCourseId,courseId);
        queryWrapper.select(EduVideo::getVideoSourceId);
        List<EduVideo> videoList = this.list(queryWrapper);

        List<String> videoSourceIdList = videoList.stream()
                .map(EduVideo::getVideoSourceId)
                .collect(toList());

        //删除阿里云多个视频
        if(!CollectionUtils.isEmpty(videoSourceIdList)){
            vodClient.deleteBatch(videoSourceIdList);
        }

        boolean removeResult = this.remove(queryWrapper);
        if(!removeResult){
            throw GuliException.from(ResultCode.DELETE_ERROR);
        }
    }
}
